import { defineStore } from 'pinia'

// 定义用户信息接口
export interface UserState {
  username: string
  type: 'admin' | 'guest' | ''
  isLoggedIn: boolean
}

// 创建用户store
export const useUserStore = defineStore('RouterUser', {

  // 初始状态
  state: (): UserState => ({
    username: '',
    type: '',
    isLoggedIn: false
  }),

  // 计算属性
  getters: {
    // 判断是否是管理员
    isAdmin: (state) => state.type === 'admin',
    // 判断是否是游客
    isGuest: (state) => state.type === 'guest'
  },

  // 操作方法
  actions: {
    // 设置用户信息
    setUserInfo(username: string, type: 'admin' | 'guest') {
      this.username = username
      this.type = type
      this.isLoggedIn = true
    },

    // 清除用户信息（登出）
    clearUserInfo() {
      this.username = ''
      this.type = ''
      this.isLoggedIn = false
    },

    // 检查是否已登录
    checkLoginStatus() {
      return this.isLoggedIn
    }
  },

  persist: true,
})
